package com.arithmetic.version2;

import java.util.ArrayList;
import java.util.Random;


public class Exercise extends ArrayList<BinaryOperation> {

    public Exercise() {
        super();
    }
    public void generateBinaryExercise(int operationCount) {
        this.clear();
        Random rand = new Random();
        while (this.size() < operationCount) {
            BinaryOperation newOp;
            if (rand.nextBoolean()) {
                newOp = BinaryOperation.generateOperation('+');
            } else {
                newOp = BinaryOperation.generateOperation('-');
            }
            if (!containsOperation(newOp)) {
                this.add(newOp);
            }
        }
    }

    private boolean containsOperation(BinaryOperation operation) {
        for (BinaryOperation op : this) {
            if (op.equals(operation)) {
                return true;
            }
        }
        return false;
    }
    public void formateAndDisplay(int columnsPerRow) {
        for (int i = 0; i < this.size(); i++) {
            System.out.print((i + 1) + "." + this.get(i));
            if ((i + 1) % columnsPerRow == 0) {
                System.out.println();
            }
        }
        if (this.size() % columnsPerRow != 0) {
            System.out.println();
        }
    }
    public void generateSubstractExercise(int operationCount) {
        this.clear();
        while (this.size() < operationCount) {
            BinaryOperation newOp = BinaryOperation.generateOperation('-');
            if (!containsOperation(newOp)) {
                this.add(newOp);
            }
        }
    }
    public void generateAdditionExercise(int operationCount) {
        this.clear();
        while (this.size() < operationCount) {
            BinaryOperation newOp = BinaryOperation.generateOperation('+');
            if (!containsOperation(newOp)) {
                this.add(newOp);
            }
        }
    }
}